<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="eventsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="活动名称" align="center" prop="name" />
      <el-table-column label="级别" align="center" prop="level" />
      <el-table-column label="活动开始时间" align="center" prop="events_start_time" width="180">
      </el-table-column>
      <el-table-column label="活动结束时间" align="center" prop="events_end_time" width="180">
      </el-table-column>
      <el-table-column label="活动地点" align="center" prop="events_address" />
      <el-table-column label="报名时间" align="center" prop="sign_time" width="180">
      </el-table-column>
      <el-table-column label="报名费用" align="center" prop="sign_price" />
      <el-table-column label="报名单位数" align="center" prop="sign_unit" />
      <el-table-column label="参数人数" align="center" prop="join_num" />
      <el-table-column label="状态" align="center" prop="activity_status" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span>{{ scope.row.apply_status === 1 ? '已报名' : '未报名' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220">
        <template slot-scope="scope">
          <el-button size="mini" type="text" v-if="scope.row.apply_status == 1" @click="handleAdd(scope.row)"
            v-hasPermi="['system:events:edit']">添加参赛人员</el-button>
          <el-button size="mini" type="text" v-if="scope.row.apply_status == 2" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:events:edit']">报名</el-button>
          <el-button size="mini" type="text" v-if="scope.row.apply_status == 1" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:events:edit']">取消报名</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改活动对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="活动名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入活动名称" />
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <el-input v-model="form.level" placeholder="请输入级别" />
        </el-form-item>
        <el-form-item label="活动开始时间" prop="eventsTime">
          <el-date-picker clearable v-model="form.events_start_time" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择活动时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动结束时间" prop="eventsTime">
          <el-date-picker clearable v-model="form.events_end_time" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择活动时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动地点" prop="eventsAddress">
          <el-input v-model="form.eventsAddress" placeholder="请输入活动地点" />
        </el-form-item>
        <el-form-item label="报名时间" prop="signTime">
          <el-date-picker clearable v-model="form.signTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择报名时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报名费用" prop="signPrice">
          <el-input v-model="form.signPrice" placeholder="请输入报名费用" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="memberopen" width="800px">
      <el-table :data="formdata" @selection-change="handleSelectionChanges">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="id" align="center" prop="id" />
        <el-table-column label="用户名" align="center" prop="name" />
        <el-table-column label="手机号" align="center" prop="phone" />
        <el-table-column label="性别" align="center" prop="sex">
          <template slot-scope="scope">
            <span>{{ scope.row.sex === 1 ? '男' : '女' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <span>{{ scope.row.sex === 1 ? '正常' : '异常' }}</span>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormdata">确 定</el-button>
        <el-button @click="membercancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addeventsmember,getapplyevents, listEvents, getEvents, delEvents, addEvents, updateEvents } from "@/api/events/events";
import {  listMember,  } from "@/api/member/member";

export default {
  name: "Events",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      //添加参数人员表单
      formdata: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动表格数据
      eventsList: [],
      // 弹出层标题
      title: "",
      memberopen: false,
      // 是否显示弹出层
      open: false,
      events_ids:'',
      //添加参数人员集合
      multipleSelection: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        type: null,
        unitId: null,
        level: null,
        eventsTime: null,
        eventsAddress: null,
        signTime: null,
        signPrice: null,
        signUnit: null,
        joinNum: null,
        status: null,
        deleteTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    handleSelectionChanges(val) {
      this.multipleSelection = val.map((v) => v.id);
    },
    submitFormdata() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请选择至少一个用户');
        return;
      }
      addeventsmember({ events_ids: this.events_ids,unit_member_id:this.multipleSelection }).then(response => {
        this.getList();
        this.memberopen = false;
      }).catch(function (error) {
        this.getList();
        this.memberopen = false;
      })
    },
    /** 查询活动列表 */
    getList() {
      this.loading = true;
      getapplyevents(this.queryParams).then(response => {
        this.eventsList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(function (error) {
        console.log(error);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    membercancel() {
      this.memberopen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        type: null,
        unitId: null,
        level: null,
        eventsTime: null,
        eventsAddress: null,
        signTime: null,
        signPrice: null,
        signUnit: null,
        joinNum: null,
        status: null,
        createTime: null,
        updateTime: null,
        deleteTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.events_ids = row.id;
      listMember(this.queryParams ).then(response => {
        this.formdata = response.rows;
        this.loading = false;
      });
      this.memberopen = true;
      this.title = "添加参数人员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEvents(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改活动";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEvents(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEvents(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除活动编号为"' + ids + '"的数据项？').then(function () {
        return delEvents(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/events/export', {
        ...this.queryParams
      }, `events_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
